#!/bin/bash

echo "🔧 更新AI增强类使用多模型系统"

# 更新头文件
cat > src/aicoder_enhancements.h << 'ENHANCEH'
#ifndef AICODER_ENHANCEMENTS_H
#define AICODER_ENHANCEMENTS_H

#include "unified_ai_client.h"
#include <string>
#include <map>
#include <vector>

class AICoderEnhancements {
public:
    AICoderEnhancements();
    
    // 模型管理
    bool SetAIModel(const std::string& model_type, const std::string& version = "");
    std::string GetModelStatus() const;
    void ListAvailableModels() const;
    
    // AI 功能
    bool ProcessAICommand(int argc, char* argv[]);
    void ShowAIHelp();
    void ShowModelHelp();
    
    std::string AnalyzeProjectStructure(const std::string& projectFile);
    std::string OptimizeMakefile(const std::string& makefilePath);
    void GenerateCodeDocumentation(const std::vector<std::string>& files);
    void StartInteractiveMode();
    
    // 直接AI调用
    std::string ChatWithAI(const std::string& message);
    
private:
    UnifiedAIClient m_aiClient;
    bool m_aiEnabled;
    
    std::vector<std::string> ParseSourceFiles(const std::string& cbpFile);
    std::string ReadFileContent(const std::string& filePath);
    
    // 模型类型转换
    AIModelType ParseModelType(const std::string& type_str);
    std::string ModelTypeToString(AIModelType type);
};

#endif // AICODER_ENHANCEMENTS_H
ENHANCEH

# 更新实现文件
cat > src/aicoder_enhancements.cpp << 'ENHANCECPP'
#include "aicoder_enhancements.h"
#include <iostream>
#include <fstream>
#include <sstream>

AICoderEnhancements::AICoderEnhancements() 
    : m_aiEnabled(true)
{
}

bool AICoderEnhancements::SetAIModel(const std::string& model_type, const std::string& version) {
    AIModelType type = ParseModelType(model_type);
    if (type == AIModelType::DSAI && model_type != "0" && model_type != "dsai") {
        std::cerr << "未知的模型类型: " << model_type << std::endl;
        return false;
    }
    
    bool success = m_aiClient.SetModel(type, version);
    if (success) {
        std::cout << "✅ 已切换到模型: " << m_aiClient.GetModelStatus() << std::endl;
    } else {
        std::cerr << "❌ 模型切换失败: " << model_type << " " << version << std::endl;
    }
    return success;
}

std::string AICoderEnhancements::GetModelStatus() const {
    return m_aiClient.GetModelStatus();
}

void AICoderEnhancements::ListAvailableModels() const {
    std::cout << "🤖 可用AI模型:\n";
    std::cout << "0. DeepSeek AI (默认)\n";
    std::cout << "   - DS-V3 (DeepSeek V3)\n";
    std::cout << "   - DS-V3.2exp0922 (DeepSeek V3.2 Experimental 0922)\n";
    std::cout << "   - DS-R1-0528 (DeepSeek R1 0528)\n";
    std::cout << "   - DS-Coder (DeepSeek Coder)\n";
    std::cout << "1. UESOFT AI\n";
    std::cout << "   - UE-Base (UESOFT Base Model)\n";
    std::cout << "   - UE-Dev (UESOFT Development)\n";
    std::cout << "   - UE-CodeExpert (UESOFT Code Expert)\n";
    std::cout << "2. OpenAI\n";
    std::cout << "   - GPT-4 (OpenAI GPT-4)\n";
    std::cout << "   - GPT-3.5 (OpenAI GPT-3.5 Turbo)\n";
    std::cout << "   - GPT-4o (OpenAI GPT-4 Omni)\n";
}

bool AICoderEnhancements::ProcessAICommand(int argc, char* argv[]) {
    if (argc < 2) {
        ShowAIHelp();
        return false;
    }
    
    std::string command = argv[1];
    
    if (command == "--ai-help") {
        ShowAIHelp();
        return true;
    }
    else if (command == "--ai-models") {
        ListAvailableModels();
        return true;
    }
    else if (command == "--ai-model-status") {
        std::cout << m_aiClient.GetModelStatus() << std::endl;
        return true;
    }
    else if (command == "--ai-set-model" && argc >= 3) {
        std::string model_type = argv[2];
        std::string version = argc >= 4 ? argv[3] : "";
        return SetAIModel(model_type, version);
    }
    else if (command == "--ai-analyze" && argc >= 3) {
        std::string projectFile = argv[2];
        std::string analysis = AnalyzeProjectStructure(projectFile);
        std::cout << "🔍 AI 项目分析结果:\n" << analysis << std::endl;
        return true;
    }
    else if (command == "--ai-optimize" && argc >= 3) {
        std::string makefile = argv[2];
        std::string optimization = OptimizeMakefile(makefile);
        std::cout << "🚀 AI Makefile 优化:\n" << optimization << std::endl;
        return true;
    }
    else if (command == "--ai-comment" && argc >= 3) {
        std::vector<std::string> files;
        for (int i = 2; i < argc; i++) {
            files.push_back(argv[i]);
        }
        GenerateCodeDocumentation(files);
        return true;
    }
    else if (command == "--ai-chat" && argc >= 3) {
        std::string message;
        for (int i = 2; i < argc; i++) {
            if (i > 2) message += " ";
            message += argv[i];
        }
        std::string response = ChatWithAI(message);
        std::cout << "🤖 AI 响应:\n" << response << std::endl;
        return true;
    }
    else if (command == "--ai-interactive") {
        StartInteractiveMode();
        return true;
    }
    else {
        std::cerr << "未知的 AI 命令: " << command << std::endl;
        ShowAIHelp();
        return false;
    }
}

void AICoderEnhancements::ShowAIHelp() {
    std::cout << "🤖 cbp2make_ai AI 功能命令:\n";
    std::cout << "  --ai-help                显示此帮助信息\n";
    std::cout << "  --ai-models              列出可用AI模型\n";
    std::cout << "  --ai-model-status        显示当前模型状态\n";
    std::cout << "  --ai-set-model <type> [version] 设置AI模型\n";
    std::cout << "  --ai-analyze <project>   AI分析项目结构\n";
    std::cout << "  --ai-optimize <makefile> AI优化Makefile\n";
    std::cout << "  --ai-comment <files...>  AI生成代码注释\n";
    std::cout << "  --ai-chat <message>      与AI对话\n";
    std::cout << "  --ai-interactive         启动交互式AI模式\n";
    std::cout << "\n模型类型:\n";
    std::cout << "  0 或 dsai    - DeepSeek AI (默认)\n";
    std::cout << "  1 或 ueai    - UESOFT AI\n";
    std::cout << "  2 或 opai    - OpenAI\n";
    std::cout << "\n示例:\n";
    std::cout << "  cbp2make_ai --ai-set-model 0 DS-V3.2exp0922\n";
    std::cout << "  cbp2make_ai --ai-set-model 1 UE-CodeExpert\n";
    std::cout << "  cbp2make_ai --ai-set-model 2 GPT-4\n";
    std::cout << "  cbp2make_ai --ai-analyze project.cbp\n";
    std::cout << "  cbp2make_ai --ai-chat \"如何优化C++构建性能?\"\n";
}

// 其他现有函数实现保持不变...
// [这里包含之前的所有其他函数实现]

AIModelType AICoderEnhancements::ParseModelType(const std::string& type_str) {
    if (type_str == "0" || type_str == "dsai") {
        return AIModelType::DSAI;
    } else if (type_str == "1" || type_str == "ueai") {
        return AIModelType::UEAI;
    } else if (type_str == "2" || type_str == "opai") {
        return AIModelType::OPAI;
    }
    return AIModelType::DSAI; // 默认
}

std::string AICoderEnhancements::ModelTypeToString(AIModelType type) {
    switch (type) {
        case AIModelType::DSAI: return "DeepSeek AI";
        case AIModelType::UEAI: return "UESOFT AI";
        case AIModelType::OPAI: return "OpenAI";
        default: return "Unknown";
    }
}

// 保留之前的函数实现...
// [这里应该包含之前的所有函数实现，如StartInteractiveMode等]
ENHANCECPP

echo "✅ AI增强类已更新为多模型系统"
echo ""
echo "🚀 新命令示例:"
echo "  ./bin/cbp2make_ai --ai-models"
echo "  ./bin/cbp2make_ai --ai-set-model 0 DS-V3.2exp0922"
echo "  ./bin/cbp2make_ai --ai-set-model 1 UE-CodeExpert"
echo "  ./bin/cbp2make_ai --ai-model-status"
